/**
 * 文  件  名：  ActivityDaoImpl.java
 * 工  程  名：  7You
 * 创建日期：  2013-10-13
 * 创建作者：  杨  强
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IActivityDao;
import com.qiyou.entity.Activity;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 活动数据操作实现类
 * 
 * @author 杨 强
 * 
 */
public class ActivityDaoImpl implements IActivityDao {
    private Connection conn = null;

    public ActivityDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Activity activity) throws SQLException {
        boolean result = false;
        String sql = "insert into `tb_activity`(`userID`, `acDate`, `acPlace`, `acName`, `acContent`, `acStartDate`, `acPathPic`,`max`) values(?, ?, ?, ?, ?, ?, ?, ?)";
        Object[] params = TransformUtil.beanTOArray(activity, true);
        params[1] = new Timestamp(activity.getAcDate().getTime());
        params[5] = new Timestamp(activity.getAcStartDate().getTime());
        result = DBUtil.execute(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(Activity activity) throws SQLException {
        boolean result = false;
        String sql = "update `tb_activity` set `acID` = ?, `userID` = ?, `acDate` = ?, `acPlace` = ?, `acName` = ?, `acContent` = ?, `acStartDate` = ?, `acPathPic` = ?, `max` = ? where `acID` = ?";
        Object[] params = TransformUtil.beanTOArray(activity, false);
        params[2] = new Timestamp(activity.getAcDate().getTime());
        params[6] = new Timestamp(activity.getAcStartDate().getTime());
        result = DBUtil.execute(conn, sql, params, params[0]);
        return result;
    }

    @Override
    public boolean doDelete(long activityID) throws SQLException {
        boolean result = false;
        String sql = "delete from `tb_activity` where `acID` = ?";
        result = DBUtil.execute(conn, sql, activityID);
        return result;
    }

    @Override
    public Activity getActivity(long activityID) throws SQLException {
        Activity result = null;
        String sql = "select * from `tb_activity` where `acID` = ?";

        List<Object[]> results = DBUtil.query(conn, sql, activityID);
        if (results.size() > 0) {
            result = (Activity) TransformUtil.arrayToBean(results.get(0),
                    Activity.class);
        }

        return result;
    }

    @Override
    public List<Activity> getActivities(String userID) throws SQLException {
        List<Activity> result = null;
        String sql = "select * from `tb_activity` where `userID` = ?";

        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<Activity>(all.size());
            for (Object[] temp : all) {
                Activity activity = new Activity();
                activity = (Activity) TransformUtil.arrayToBean(temp,
                        Activity.class);
                result.add(activity);
            }
        }
        return result;
    }

    @Override
    public List<Activity> getActivities(Object... params) throws SQLException {
        List<Activity> result = null;
        StringBuffer sql = new StringBuffer("select * from `tb_activity` ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (all.size() > 0) {
            result = new ArrayList<Activity>();
            for (Object[] temp : all) {
                Activity activity = new Activity();
                activity = (Activity) TransformUtil.arrayToBean(temp,
                        Activity.class);
                result.add(activity);
            }
        }
        return result;
    }

}
